package com.yawn;

import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * @author Created by yawn on 2017-12-09 10:00
 */
public class FirstTest {

    public static void main(String[] args) {

        // 0.0 读取流程引擎配置
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti.cfg.xml", "configBean2");
        // 0.1.1 获得流程引擎(按自定义配置)
        ProcessEngine engine = configuration.buildProcessEngine();
        /// 0.1.2 获得流程引擎(按默认配置)
//        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        // 1 将流程持久化
        RepositoryService repositoryService = engine.getRepositoryService();
        // 2 流程启动等
        RuntimeService runtimeService = engine.getRuntimeService();
        // 3 处理任务
        TaskService taskService = engine.getTaskService();


        // 1 部署流程文件
        repositoryService.createDeployment().addClasspathResource("first.bpmn").deploy();
        // 2 启动流程，返回一个流程实例
        ProcessInstance myProcess = runtimeService.startProcessInstanceByKey("myProcess_1");
        // 3 查询当前的流程任务
        Task task = taskService.createTaskQuery().processInstanceId(myProcess.getId()).singleResult();
        System.out.println("当前流程任务是： " + task);
        // 3 完成当前任务
        taskService.complete(task.getId());
        // 3 查询当前的流程任务
        task = taskService.createTaskQuery().processInstanceId(myProcess.getId()).singleResult();
        System.out.println("当前流程任务是： " + task);
        // 3 完成当前任务
        taskService.complete(task.getId());
        // 3 查询当前的流程任务
        task = taskService.createTaskQuery().processInstanceId(myProcess.getId()).singleResult();
        System.out.println("当前流程任务是： " + task);


        engine.close();
    }
}
